const App = getApp()
const db = wx.cloud.database()
const _ = db.command
let that
Page({

  /**
   * 页面的初始数据
   */
  data: {
    show: false,
    // 输入框高度
    inputHeight: {
      minHeight: 150
    },

    pickerIdx: null,
    picker: ['优秀', '良好', '及格', '不及格'],
    // 老师的评语
    Comment: "",
    // 老师给的成绩
    results: "",
    // 实践任务详情
    practiceDetails:"",
    // 成员实践任务详情
    homework:"",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    that = this
    let data = JSON.parse(decodeURIComponent(options.passData));
    console.log('获取传值过来的同学的学号和实践任务id', data);
    let UserInfo = App.cache.getUserInfo()
    console.log('获取用户信息:', UserInfo);
    delete UserInfo._id
    that.setData({
      practiceId: data.practiceid,
      memberAccount: data.id,
      UserInfo:UserInfo
    })
    // 获取实践任务详情
    that.getPracticeDetails()
    // 获取该同学提交的实践作业
    that.getMemberPractice()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  // 成绩
  bindPickerChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    let picker = that.data.picker
    that.setData({
      pickerIdx: e.detail.value,
      results: picker[e.detail.value]
    })
  },
  // 评阅弹窗打开
  showPopup() {
    this.setData({
      show: true
    });
  },
  // 关闭评阅弹窗
  onClose() {
    this.setData({
      show: false
    });
  },
  // input 
  onChange(event) {
    console.log(event.detail);
    that.setData({
      Comment: event.detail
    })
  },
    // 关闭提示弹窗
    hideModal(e) {
      this.setData({
        modalName: null
      })
    },
    // 预览照片
    previewImage(e) {
      console.log('预览', e.currentTarget.dataset.imgpath);
      let url = e.currentTarget.dataset.imgpath;
      wx.previewImage({
        current: url,
        urls: [url]
      })
    },
      // 下载文件到本地
  downloadFile(e) {
    let fileid = e.currentTarget.dataset.fileid
    App.toast.showLoading('下载中')
    wx.cloud.getTempFileURL({
      fileList: [{
        fileID: fileid,
      }]
    }).then(res => {
      App.toast.hideLoading()
      console.log('返回来的链接', res.fileList[0])
      that.setData({
        modalName: "DialogModal1",
        downloadFilePath: res.fileList[0].tempFileURL,
      })
    }).catch(error => {
      console.error(error)
    })
  },
  // 复制文件链接地址
  codeFilePath(e) {
    console.log(e.currentTarget.dataset);
    let filepath = e.currentTarget.dataset.filepath
    wx.setClipboardData({
      data: filepath,
      success: res => {
        console.log(res);
        App.toast.showToast('地址已经复制', 'none', 1000)
        that.hideModal()
      }
    })
  },
  // 获取实践任务详情
  getPracticeDetails(){
    App.toast.showLoading('加载中')
    db.collection('practice').where({
      practiceId: that.data.practiceId
    }).get({
      success: function (res) {
        App.toast.hideLoading()
        console.log(res.data[0])
        let practiceDetails = res.data[0]
        that.setData({
          practiceDetails: practiceDetails
        })
      }
    })
  },
  // 获取该同学提交的实践作业
  getMemberPractice() {
    App.toast.showLoading('加载中')
    db.collection('practice').where(_.and([{
      practiceId: that.data.practiceId
    }, {
      'courseMember.account': _.in([that.data.memberAccount])
    }])).get({
      success: function (res) {
        App.toast.hideLoading()
        console.log(res.data[0])
        let list = res.data[0]
        let homework = list.courseMember.filter(item => {
          return that.data.memberAccount === item.account
        })
        console.log('获得该同学的是实践详情', homework);
        that.setData({
          homework: homework[0]
        })
      }
    })
  },
  // 提交评语和成绩
  submitComment() {
    let results = that.data.results
    if (results == '') {
      App.toast.showToast('成绩不能为空')
      return false
    }
    App.toast.showLoading('提交中')
    let Comment = that.data.Comment
    console.log(results, Comment);
    db.collection('practice').where(_.and([{
        practiceId: that.data.practiceId
      },
      {
        'courseMember.account': _.in([that.data.memberAccount])
      }
    ])).update({
      data: {
        'courseMember.$.Review': true,
        'courseMember.$.Comment': Comment,
        'courseMember.$.results': results,
      },
      success: function (res) {
        console.log('提交成功', res);
        App.toast.showToast('提交成功')
        App.route.relBack('/pages/teachers/details/index')
      }
    })
  },
})